﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class AppraiseTeacher
    {
        private int _teacherid;
        private string _appraisename;
        private string _teachcode;
        private string _teachname;
        private int _sexy;
        private DateTime _birthday;

        public int TeacherID
        {
            get { return _teacherid; }
            set { _teacherid = value; }
        }

        public string AppraiseName
        {
            get { return _appraisename; }
            set { _appraisename = value; }
        }

        public string TeachCode
        {
            get { return _teachcode; }
            set { _teachcode = value; }
        }

        public string TeachName
        {
            get { return _teachname; }
            set { _teachname = value; }
        }

        public int Sexy
        {
            get { return _sexy; }
            set { _sexy = value; }
        }

        public DateTime Birthday
        {
            get { return _birthday; }
            set { _birthday = value; }
        }
    }
    public class Appraise : BaseBR
    {
        public bool Add(t_Appraise model, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                //效验重名
                int checkName = (from a in db.t_Appraise
                                 where a.AppraiseName == model.AppraiseName
                                 select a).Count();
                if (checkName > 0)
                {
                    msg = "已经存在名称为【" + model.AppraiseName + "】的分类名称.";
                    result = false;
                }
                if (result)
                {
                    db.t_Appraise.AddObject(model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败！";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool Update(t_Appraise model, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                //效验重名
                int checkName = (from a in db.t_Appraise
                                 where a.AppraiseName == model.AppraiseName && a.RID != model.RID
                                 select a).Count();
                if (checkName > 0)
                {
                    msg = "已经存在名称为【" + model.AppraiseName + "】的分类名称.";
                    result = false;
                }
                if (result)
                {
                    db.t_Appraise.FirstOrDefault(t => t.RID == model.RID);
                    db.ApplyCurrentValues("t_Appraise", model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool Delete(int rid, ref string msg)
        {
            using (DBEntities db = new DBEntities())
            {
                t_Appraise model = db.t_Appraise.FirstOrDefault(t => t.RID == rid);
                db.DeleteObject(model);
                try
                {
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                    msg = "操作失败";
                    return false;
                }
            }
        }

        public IList<t_Appraise> ListAll()
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from a in db.t_Appraise
                           select a);
                return obj.ToList<t_Appraise>();
            }
        }

        public IList<AppraiseTeacher> ListTeacher(int FType, string TeachCode, string TeachName)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from b in db.TEACHERINFO
                           join c in db.t_AppraiseRelation on b.RID equals c.TeacherID into temp1
                           from d in temp1.DefaultIfEmpty()
                           join a in db.t_Appraise on d.AppraiseID equals a.RID into temp2
                           from e in temp2.DefaultIfEmpty()
                           where (FType == -1 ? true : (FType == 0 ? e.RID == null : e.RID == FType))
                           && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.Contains(TeachCode.Trim()))
                           && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.Contains(TeachName.Trim()))
                           && b.TEACHCODE != "admin"
                           select new AppraiseTeacher
                           {
                               TeacherID = b.RID,
                               AppraiseName = e.AppraiseName,
                               TeachCode = b.TEACHCODE,
                               TeachName = b.TEACHNAME,
                               Sexy = (int)b.SEXY != null ? (int)b.SEXY : 0,
                               Birthday = (DateTime)b.BIRTHDAY != null ? (DateTime)b.BIRTHDAY : System.DateTime.MaxValue
                           });
                return obj.ToList();
            }
        }
    }
}
